﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Janus.Windows.GridEX;

namespace UControls
{
    public partial class ctrlVehicleID : UserControl
    {
        public new event EventHandler TextChanged;

        public ctrlVehicleID()
        {
            InitializeComponent();

            this.edtVehicleID.KeyDown += new KeyEventHandler(edtVehicleID_KeyDown);
            this.edtVehicleID2.KeyDown += new KeyEventHandler(edtVehicleID2_KeyDown);
            this.edtVehicleID.TextChanged += new EventHandler(edtVehicleID_TextChanged);
            this.edtVehicleID2.TextChanged += new EventHandler(edtVehicleID_TextChanged);
        }

        void edtVehicleID_TextChanged(object sender, EventArgs e)
        {
            TextChanged.Invoke(this, e);
        }

       void edtVehicleID2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back && edtVehicleID2.TextLength == 0)
            {
                edtVehicleID.Focus();
                edtVehicleID.SelectionStart = edtVehicleID.TextLength;
            }
        }

        void edtVehicleID_KeyDown(object sender, KeyEventArgs e)
        {
            if (edtVehicleID.TextLength == 2 && Char.IsLetterOrDigit(Convert.ToChar(e.KeyCode)))
            {
                edtVehicleID2.Focus();
            }
        }

        public new string Text
        {
            get {
                string result = String.Concat(this.edtVehicleID.Text, this.edtVehicleID2.Text);
                if (result.Trim() != String.Empty)
                {
                    result = String.Concat(this.edtVehicleID.Text, '-', this.edtVehicleID2.Text);
                }
                return result; }
            set
            {
                string result = String.Concat(this.edtVehicleID.Text, '-',  this.edtVehicleID2.Text);

                if (result != value)
                {
                    string[] arr = value.Split('-');
                    if (arr.Length > 1)
                    {
                        this.edtVehicleID.Text = arr[0];
                        this.edtVehicleID2.Text = arr[1];
                    }
                    else
                    {
                        this.edtVehicleID.Text = value;
                        this.edtVehicleID2.Text = String.Empty;
                    }
                }
            }
        }

        public int TextLength
        {
            get { return Text.Length; }
        }

        private VisualStyle VisualStyle
        {
            get { return edtVehicleID.VisualStyle; }
            set {
                if (edtVehicleID.VisualStyle != value)
                {
                    edtVehicleID.VisualStyle = value;
                    edtVehicleID2.VisualStyle = value;
                }
            }
        }

        public new bool Focus()
        {
            edtVehicleID.Focus();
            edtVehicleID.SelectionStart = 0;

            return true;
        }
    }
}
